import request from "@/utils/http";

export default () => {
  // 正常 的设置 options
  const setOptions = ({
    api = "",
    method = "get",
    params = {},
    optionsList = [],
    valueKey = "",
    labelKey = "",
  }) => {
    return new Promise(async (resolve) => {
      const { data: res } = await request[method](
        api,
        method === "get" ? { params } : params
      );

      // request({
      //   url: api,
      //   method: method,
      //   data: params,
      //   isParams: method === "get" ? true : false,
      // });

      const list = res.data || [];

      optionsList.length = 0;

      list.forEach((e) => {
        optionsList.push({
          label: labelKey === "item" ? e : e[labelKey],
          value: valueKey === "item" ? e : e[valueKey],
        });
      });

      resolve();
    });
  };
  /**
   * @author xuzy
   * @time 2022-11-11
   * 如果是使用了级联的表头 hooks
   * @param {XTableHeader Options Config} cascadeList
   * @param {XTableHeader Bind Form} tableHeaderForm
   */
  const useCascade = ({ cascadeList = [], tableHeaderForm = {} }) => {
    cascadeList.forEach((e) => {
      if (!e.isCascade) return; //不是级联就返回
      let mTableHeaderFormKey = tableHeaderForm[e.prop] || ""; // 值记录
      // 监听绑定数据的变化
      Object.defineProperty(tableHeaderForm, e.prop, {
        set: function (newValue) {
          mTableHeaderFormKey = newValue;

          //如果需要 label值  改为数组是为了契合新老接口的值
          if (e.turnKey && Array.isArray(e.turnKey)) {
            const find = e.options.find((te) => te.value === newValue);

            e.turnKey.map((_tk) => {
              if (find) {
                this[_tk] = find.label;
              } else {
                this[_tk] && delete this[_tk];
              }
            });
          }

          // 需要改变对象键的数组List
          let changeSelectObjList = [];

          // 如果changeKey 是一个数组 （要改变多个）
          if (Array.isArray(e.changeKey)) {
            changeSelectObjList = [...e.changeKey];
          } else {
            changeSelectObjList = [e.changeKey];
          }

          e.changeKey &&
            changeSelectObjList.forEach((changeKey) => {
              // 获取改变key的对象值
              const changeSelectObj = cascadeList.find(
                (_e) => _e.prop === changeKey
              );

              if (!changeSelectObj) return; // 如果找不到就返回 防止因为COPY的原因漏写而报错

              // 将级联参数封装成一个对象
              const params = {};
              // 这是取表头对象内的参数值
              changeSelectObj.params &&
                changeSelectObj.params.forEach((_e) => {
                  params[_e.requestKey] = tableHeaderForm[_e.formKey];
                });

              // 清空下一级的绑定值
              changeSelectObj.options.length = 0;
              tableHeaderForm[changeSelectObj.prop] = undefined;
              // 然后触发setOptions值
              tableHeaderForm[e.prop] != undefined &&
                setOptions({
                  api: changeSelectObj.getApi,
                  method: changeSelectObj.getMethod,
                  params: params,
                  optionsList: changeSelectObj.options,
                  valueKey: changeSelectObj.valueKey,
                  labelKey: changeSelectObj.labelKey,
                });
            });
        },
        get: function () {
          return mTableHeaderFormKey;
        },
      });
    });
  };

  return {
    setOptions,
    useCascade,
  };
};
